package com.hengxinguotong.zhihuichengjian.widget.view;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.adapter.SelectListAdapter;
import com.hengxinguotong.zhihuichengjian.utils.Utils;

import java.util.List;

/**
 * Created by liux on 16/3/15.
 */
public class SelectListPop extends PopupWindow {

    private final LinearLayout popRoot;
    private ListView dialogImgdirNameLv;
    private OnDirListPopItemClicked listener;
    private View blackView;
    private String[]strings;

    public SelectListPop(Activity act,int layoutID, String[] strings, OnDirListPopItemClicked listener) {
        super(act);
        this.listener = listener;
        this.strings = strings;
        popRoot = (LinearLayout) LayoutInflater.from(act).inflate(layoutID, null);
        setAnimationStyle(R.style.dialogSelectAnimat);
        setWidth(Utils.getWindowWidth(act));
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setContentView(popRoot);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setOutsideTouchable(true);
        initView(act, strings);
    }

    private void initView(Context context, String[] strings) {
        dialogImgdirNameLv = (ListView) popRoot.findViewById(R.id.dialog_imgdir_name_lv);
        blackView = popRoot.findViewById(R.id.dialog_imgdir_name_view);
        dialogImgdirNameLv.setOnItemClickListener(itemClickListener);
        SelectListAdapter adapter = new SelectListAdapter(context, strings);
        dialogImgdirNameLv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        blackView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (listener == null) {
                return;
            }
            SelectListAdapter adapter = (SelectListAdapter) parent.getAdapter();
            listener.onPopItemClick(position, adapter.getItem(position).toString());
            dismiss();
        }
    };


    public interface OnDirListPopItemClicked {
        void onPopItemClick(int position, String name);

    }
}
